package org.example.study.redis;

import com.google.protobuf.ServiceException;
import io.netty.util.BooleanSupplier;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.example.redis.RedisLockUtil;
import org.example.redis.lock.CheckedSupplier;
import org.example.redis.lock.LockType;
import org.example.redis.lock.RedisLockClient;
import org.example.study.sc.service.GoodsStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @program: rabbitmq-study
 * @description:
 * @Version V1.0.0
 * @author: dazhi
 * @create: 2021-10-14 15:20
 **/
@RestController
public class TestRedisController {
    @Autowired
    private RedisTemplate redisTemplate;


    @Autowired
    RedisLockUtil redisLockUtil;
    @Autowired
    GoodsStockService goodsStockService;
    @Autowired
    RedisLockClient redisLockClient;

    @Autowired


    @GetMapping("testLock")
    public String testRedissonLock() {
        Supplier<String> checkedSupplier = () -> getServiceTest();
        String xx = redisLockClient.lock("xx", LockType.REENTRANT, 1, 100, TimeUnit.SECONDS, checkedSupplier);
        return xx;
    }

    public String getServiceTest() {
        try {
            Thread.sleep(100000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("1");
        return "String";
    }

    @GetMapping("valueSet")
    public String getList() {
        redisTemplate.opsForValue().set("stringValue", "a");
        redisTemplate.opsForValue().append("stringValue", "aaa");
        return "OK";
    }

    @GetMapping("testRedisLock")
    public Boolean testRedisLock() {
        return redisLockUtil.testRedisLock("testLock1", String.valueOf(Thread.currentThread().getId()));


    }

}
